package br.com.seosoft.simplecalcfinan;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class LoginActivity extends SherlockFragmentActivity  {

	public static final int REQUEST_CODE_PLUS_CLIENT_FRAGMENT = 0;
	private String SHA = "3C:20:CE:A1:87:07:42:E6:99:EE:36:CD:CB:EC:86:1D:FA:0B:5C:19";

	private String version;
	private TaskPostUsuario taskPostUsuario;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getSupportActionBar().hide();
		setContentView(R.layout.activity_login);

		PackageInfo pInfo;
		try {
			pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
			version = pInfo.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			Log.e(geral.LogCat, "erro obter nome pacote: " + e.getMessage());
			Tracker myTracker = EasyTracker.getTracker(); // Get a reference to
			// tracker.
			myTracker.sendException(e.getMessage(), false); // false indicates
			// non-fatal
			// exception.
		}

		

		TextView version = (TextView) findViewById(R.id.tvVersion);

		version.setText(version.getText() + this.version);

	
		findViewById(R.id.tvPolitica).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				linkPolitica();

			}
		});

		// findViewById(R.id.sign_in_button).setOnClickListener(
		// new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// mSignInFragment.signIn(REQUEST_CODE_PLUS_CLIENT_FRAGMENT);
		// }
		// });
		//
		// findViewById(R.id.btnSair).setOnClickListener(
		// new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// mSignInFragment.revokeAccessAndDisconnect();
		// }
		// });

	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		getSupportActionBar().hide();
		window.setFormat(PixelFormat.RGBA_8888);
	}

	

	private void executePostUsuario(Usuario user) {
		// TODO Auto-generated method stub

		if (taskPostUsuario == null) {
			taskPostUsuario = new TaskPostUsuario();
			taskPostUsuario.execute(user);
		} else {
			taskPostUsuario.cancel(true);
			taskPostUsuario = new TaskPostUsuario();
			taskPostUsuario.execute(user);
		}

	}

	private void linkPolitica() {
		// TODO Auto-generated method stub

		Intent it = new Intent(
				Intent.ACTION_VIEW,
				Uri.parse("http://duanniston.wix.com/simplecalcfinan#!politica-de-privacidade/c1n7g"));
		startActivity(it);

	}

	

	

	private class TaskPostUsuario extends AsyncTask<Usuario, Void, Usuario> {

//		private RestTemplate rest;
//		private MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
		private Gson gson;
		Usuario user;
		Usuario[] userVector;
		// Set the Content-Type header

		private String response;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		
//			rest = new RestTemplate(true);
//			
//
//			rest.getMessageConverters().add(new GsonHttpMessageConverter());
//			rest.getMessageConverters().add(new StringHttpMessageConverter());
		}

		@Override
		protected Usuario doInBackground(Usuario... params) {
			
			// TODO Auto-generated method stub

//			if (!isCancelled()) {
//				try {
//
//					userVector = gson.fromJson(rest.getForObject(geral.URL
//							+ geral.URL_USUARIO + geral.URl_ID_USUARIO_PLUS
//							+ params[0].getId_googleplus(), String.class),
//							Usuario[].class);
//
//					if (Arrays.asList(userVector).isEmpty()) {
//
//						response = rest.postForObject(geral.URL
//								+ geral.URL_USUARIO, gson.toJson(params[0]),
//								String.class);
//						user = gson.fromJson(response, Usuario.class);
//						return user;
//					} else {
//						return userVector[0];
//					}
//
//				} catch (RestClientException e) {
//					// TODO Auto-generated catch block
//
//					Log.e(geral.LogCat, "Error Post Usuario", e);
//					return null;
//				}
//			} else {
//				return null;
//			}
			return user;
		}

		@Override
		protected void onPostExecute(Usuario result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			if (!isCancelled()) {
				if (result != null) {
					Log.v(geral.LogCat, "email: " + result.getEmail());
					
					Intent it = new Intent(LoginActivity.this, inicial.class);

					startActivity(it);
					finish();
				} else {
					Log.d(geral.LogCat, "Erro ao cadastrar usuario: ");
					
				}
			} else {
				Log.v(geral.LogCat, "isCancelled");
			}

		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		taskPostUsuario.cancel(true);
	}

}
